package com.graduation.mybatis.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation.mybatis.bean.*;
import com.graduation.mybatis.mapper.FoodEntriesMapper;
import com.graduation.mybatis.service.DietRecordService;
import com.graduation.mybatis.mapper.DietRecordMapper;
import jakarta.annotation.Resource;
import org.apache.ibatis.javassist.compiler.ast.Pair;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 23726
* @description 针对表【diet_record】的数据库操作Service实现
* @createDate 2025-04-07 18:59:01
*/
@Service
public class DietRecordServiceImpl extends ServiceImpl<DietRecordMapper, DietRecord>
    implements DietRecordService{

    @Resource
    private DietRecordMapper dietRecordMapper;


    public boolean removeByrecordIdAndFoodEntryId(Long foodEntryId) {
        dietRecordMapper.deleteDietRecordByFoodEntryId(foodEntryId);
        dietRecordMapper.deleteFoodEntryById(foodEntryId);
        return true;
    }

    // 根据用户ID和餐次类型查询饮食记录
    public List<DietRecordVO> getDailyRecords(Long userId, Integer mealType, LocalDate date) {
        return dietRecordMapper.selectDailyRecords(userId, mealType, date);
    }

    public NutritionSummary calculateNutritionSummary(List<DietRecordVO> records) {
        NutritionSummary summary = new NutritionSummary();
        if (records == null || records.isEmpty()) {
            return summary;
        }
        records.forEach(record -> {
            summary.setTotalCalories(summary.getTotalCalories() + record.getCalories());
            summary.setTotalProtein(summary.getTotalProtein() + record.getProtein());
            summary.setTotalFat(summary.getTotalFat() + record.getFat());
            summary.setTotalCarbohydrate(summary.getTotalCarbohydrate() + record.getCarbohydrate());
        });

        return summary;
    }

    // 根据用户ID查询所有饮食记录
    public List<DietRecordVO> getRecordsByUserId(Long userId, LocalDate date) {
        return dietRecordMapper.selectDailyRecordsByUserId(userId,date);
    }

    public Map<Integer, NutritionSummary> getDailyMealSummaries(Long userId, LocalDate date) {
        Map<Integer, NutritionSummary> summaries = new HashMap<>();

        // 只查询实际存在的餐次数据
        List<DietRecordVO> allRecords = dietRecordMapper.selectAllDailyRecords(userId, date);

        // 按餐次分组
        Map<Integer, List<DietRecordVO>> recordsByMeal = allRecords.stream()
                .collect(Collectors.groupingBy(DietRecordVO::getMealType));

        // 只为有记录的餐次创建汇总
        recordsByMeal.forEach((mealType, records) -> {
            summaries.put(mealType, calculateNutritionSummary(records));
        });

        return summaries;
    }

    public Map<Integer, NutritionSummary> getPeriodyMealSummaries(Long userId, Integer period, String date) {
        Map<Integer, NutritionSummary> summaries = new HashMap<>();

        // 只查询实际存在的餐次数据
        List<DietRecordVO> allRecords = dietRecordMapper.selectPeriodyMealSummaries(userId, period, date);
        // 按餐次分组
        Map<Integer, List<DietRecordVO>> recordsByMeal = allRecords.stream()
                .collect(Collectors.groupingBy(DietRecordVO::getMealType));

        // 只为有记录的餐次创建汇总
        recordsByMeal.forEach((mealType, records) -> {
            summaries.put(mealType, calculateNutritionSummary(records));
        });

        return summaries;
    }

}




